<?php
/**
 * @file
 * Download Monitor - Allows you to trace file downloads. Just place the direct link
 * to the file on the web, and specify the extensions of files you want to trace in the GUI.
 * It creates interactive chart and records downloads for today, yesterday, this week,
 * last week, this month, last month and total.
 * Requires writable .htaccess file of Apache Web server in Joomla root folder.
 * @package download-monitor
 * @version 1.0
 * @revision @REVISION@
 * @license http://www.gnu.org/licenses/gpl-3.0.txt     GNU General Public License v3
 * @copyright (C) 2012 by CodeGravity.com - All rights reserved!
 * @website http://www.codegravity.com
 */

if($task=='add')
{

    if (isset($_POST['Submit']) && $_POST['Submit']=="Add")
    {
        $extname = trim(mysql_escape_string($_POST['extname']));

        $extensionquery_ht_prev = sprintf("SELECT * FROM #__download_monitor_extension");
        $db->setQuery($extensionquery_ht_prev);
        $extensionar_ht_prev = $db->loadRowList();

        foreach($extensionar_ht_prev as $extensionhtprev)
        {
            $ext_n_prev = $ext_n_prev.$extensionhtprev[1]."|";
        }
        $ext_n_prev = substr($ext_n_prev,0,strlen($ext_n_prev)-1);



        $extensionquery_add = sprintf("insert into #__download_monitor_extension (extname) values ('%s')", mysql_escape_string($extname));
        $db->setQuery($extensionquery_add);
        $db->query();

        $extensionquery_ht = sprintf("SELECT * FROM #__download_monitor_extension");
        $db->setQuery($extensionquery_ht);
        $extensionar_ht = $db->loadRowList();

        foreach($extensionar_ht as $extensionht)
        {
            $ext_n = $ext_n.$extensionht[1]."|";
        }
        $ext_n = substr($ext_n,0,strlen($ext_n)-1);

        $findpath = (JURI::root());
        $p = parse_url( $findpath );
        $path = $p['path'];

        $writingonht_prev = "RewriteEngine on"."\n"."RewriteRule ^(.*).(".$ext_n_prev.")$ ".$path."index.php?option=com_download-monitor&file=$1.$2 [R,L]";

        $jp = str_replace("administrator","",JPATH_BASE);

        $root_file = $jp.".htaccess";

        $existingcode = file_get_contents($root_file);

        $existingcode_f = str_replace($writingonht_prev,"",$existingcode);
        //$existingcode_f = str_replace($writingonht_prev1,"",$existingcode);

        $writingonht = $existingcode_f."RewriteEngine on";
        $writingonht = $writingonht."\n"."RewriteRule ^(.*).(".$ext_n.")$ ".$path."index.php?option=com_download-monitor&file=$1.$2 [R,L]";

        if (file_exists($root_file))
        {
            if (is_writable($root_file))
            {
                $handle = fopen($root_file,"w");
                fwrite($handle,$writingonht);
                fclose($handle);
                header("location: index.php?option=com_download-monitor");
            }
            else
            {
                echo "Your .htaccess is not Writable.";
            }
        }
        else
        {
            $handle = fopen($root_file,"w");
            if ($handle)
            {
                fwrite($handle,$writingonht);
                fclose($handle);
                header("location: index.php?option=com_download-monitor");
            }
            else
            {
                echo ".htaccess could not be created, Please create your own.";
            }
        }

    }

    ?>

<form action="" method="post" name="addform" onsubmit="return check_add();">
    <table width="83%" border="0" cellpadding="3" cellspacing="0" class="table">
        <tr>
            <td colspan="2" class="Subtitle">Add Extension </td>
        </tr>
        <tr>
            <td height="23" colspan="2" align="left" style='color: red'><b>Warning:</b> always backup your .htaccess file before adding new extension or path! Do not add "php" as monitored extension. <br/>Be also careful with adding png/jpg, the download count will be increased once someone opens your page which contains images with this extension.<br/><br/></td>
        </tr>
        <tr>
            <td width="31%">
                <div align="right">Extension Name<font color="#FF0000"> *</font>                    </div>                  </td>
            <td width="69%" align="left">
                <input name="extname" type="text" size="50" value="">                  </td>
        </tr>
        <tr class="title">
            <td height="20" colspan="2">
                <div align="center">
                    <input name="Submit" type="submit" class="button" value="Add">
                    <input name="Submit" type="button" class="button" value="Back" onclick="window.location.href='index.php?option=com_download-monitor'">
                </div>                  </td>
        </tr>
    </table>
    <br>
</form>
<?php
}
?>